package com.un.ebs.sys.uitl;

import com.un.ebs.core.enums.*;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.sys.domain.DataSource;
import com.un.ebs.sys.domain.Field;
import com.un.ebs.sys.domain.Table;

import java.util.ArrayList;
import java.util.List;

/**
 * 生成sql语句类
 *
 * @Author lcl
 * @Date 2021/01/20
 */
public class SqlCodeUtil {

    private static final Integer constraintLength = 40;

    /**
     * 建表sql
     *
     * @param list
     * @param tableName
     * @param databaseName
     * @param type
     * @return
     */
    public static String createTableSql(List<Field> list, String tableName, String databaseName, DbTypeEnum type) {
        StringBuilder builder = new StringBuilder();
        switch (type) {
            case mysql:
                builder.append("CREATE TABLE `" + tableName + "` (").append("\n");
                builder.append(baseMySql(list, tableName, type.getCode()));
                builder.append(") ENGINE=InnoDB DEFAULT CHARACTER SET=utf8mb4 ;").append("\n\n");
                break;
            case dmsql:
                builder.append("CREATE TABLE \"" + databaseName + "\".\"" + tableName + "\" (").append("\n");
                builder.append(baseDmServer(list, databaseName, type.getCode(), tableName));
                builder.append(") STORAGE(ON \"MAIN\", CLUSTERBTR) ;").append("\n\n");
                break;
            case sqlserver:
                builder.append("CREATE TABLE [dbo].[" + tableName + "] (").append("\n");
                builder.append(baseSqlServer(list, type.getCode(), tableName));
                builder.append(");").append("\n\n");
                break;
            default:
                return null;
        }
        return builder.toString();
    }

    /**
     * 删除表sql
     *
     * @param tableName
     * @param databaseName
     * @param type
     * @return
     */
    public static String delTableSql(String tableName, String databaseName, DbTypeEnum type) {
        String str;
        switch (type) {
            case mysql:
                str = "DROP TABLE " + tableName + ";";
                break;
            case dmsql:
                str = "DROP TABLE \"" + databaseName + "\".\"" + tableName + "\" ;";
                break;
            case sqlserver:
                str = "DROP TABLE [dbo].[" + tableName + "];";
                break;
            default:
                return null;
        }
        return str;
    }


    public static String isExistTable(String databaseName, String tableName, DbTypeEnum type) {
        //select count(*)  from information_schema.TABLES t where t.TABLE_SCHEMA ='ticket' and t.TABLE_NAME ='admin'
        String str;
        switch (type) {
            case mysql:
                str = "select count(*) AS count from information_schema.TABLES t where t.TABLE_SCHEMA ='" +
                        databaseName + "' and t.TABLE_NAME ='" + tableName + "'";
                break;
            case dmsql:
                //SELECT COUNT(*)  FROM all_tables WHERE   OWNER = 'agro_bp_ds' AND TABLE_NAME = 'azkaban_main'
                str = "SELECT COUNT(*) as count  FROM all_tables WHERE   OWNER = '" + databaseName + "' AND TABLE_NAME = '" + tableName + "'";
                break;
            case sqlserver:
                str = "select top 1 COUNT(*) AS count from sysObjects where Id=OBJECT_ID(N'[dbo].[" + tableName + "]') and xtype='U'";
                break;
            default:
                return null;
        }
        return str;
    }

    /**
     * 添加字段sql
     *
     * @param field
     * @param tableName
     * @param databaseName
     * @param type
     * @return
     */
    public static List<String> addFieldSql(Field field, String tableName, String databaseName, DbTypeEnum type) {
        List<String> list = new ArrayList<>();
        String head;
        switch (type) {
            case mysql:
                head = "ALTER TABLE " + tableName + " add ";
                list = addMySqlFiled(field, type.getCode(), tableName, head);
                break;
            case dmsql:
                head = "ALTER TABLE \"" + databaseName + "\".\"" + tableName + "\" add ";
                list = addDmFiled(field, type.getCode(), databaseName, tableName, head);
                break;
            case sqlserver:
                head = "ALTER TABLE [dbo].[" + tableName + "] add ";
                list = addSqlServerFiled(field, type.getCode(), tableName, head);
                break;
            default:
                return null;
        }
        return list;
    }

    /**
     * 删除字段sql
     *
     * @param field
     * @param tableName
     * @param databaseName
     * @param type
     * @return
     */
    public static List<String> delFieldSql(Field field, String tableName, String databaseName, DbTypeEnum type) {
        //alter table 表名 drop column 字段
        List<String> sql = new ArrayList<>();
        switch (type) {
            case mysql:
                //判断是否为外键
                if (field.getFiledType().getCode() == 0) {
                    sql.add("ALTER TABLE " + tableName + " drop foreign key " + tableName + "_" + field.getFiledName() + "_fk_" +
                            field.getFkTableName() + "_" + field.getFkFieldName() + ";");
                }
                sql.add("ALTER TABLE " + tableName + " drop column " + field.getFiledName() + ";");
                break;
            case dmsql:
                if (field.getFiledType().getCode() == 0) {
                    sql.add("ALTER TABLE \"" + databaseName + "\".\"" + tableName + "\" drop constraint " +
                            tableName + "_" + field.getFiledName() + "_fk_" + field.getFkTableName() + "_" + field.getFkFieldName() + ";");
                }
                sql.add("ALTER TABLE \"" + databaseName + "\".\"" + tableName + "\" drop column \"" + field.getFiledName() + "\";");
                break;
            case sqlserver:
                if (field.getFiledType().getCode() == 0) {
                    sql.add("ALTER TABLE " + tableName + " drop constraint " + tableName + "_" + field.getFiledName() + "_fk_" +
                            field.getFkTableName() + "_" + field.getFkFieldName() + ";");
                }
                if (field.getFiledType().getCode() == 7) {
                    //CONSTRAINT
                    sql.add("alter table [dbo].[" + tableName + "]  drop constraint df_" + tableName + "_" + field.getFiledName() + ";");
                }
                sql.add("ALTER TABLE [dbo].[" + tableName + "]  drop column [" + field.getFiledName() + "];");
                break;
            default:
                return null;
        }
        return sql;
    }

    /**
     * 修改字段属性,字段名
     *
     * @param field
     * @param tableName
     * @param ds
     * @return
     */
    public static List<String> updateFieldSql(Field field, String tableName, String oldFiledName, DataSource ds) {
        List<String> list = new ArrayList<>();
        String headSql;
        String fkName = tableName + "_" + field.getFiledName() + "_fk_" + field.getFkTableName() + "_" + field.getFkFieldName();
        switch (ds.getDbType()) {
            case mysql:
                //是否存在外键
                if (isExistFK(fkName, ds)) {
                    list.add("ALTER TABLE " + tableName + " drop foreign key " + tableName + "_" +
                            field.getFiledName() + "_fk_" + field.getFkTableName() + "_" + field.getFkFieldName());
                }

                headSql = "ALTER TABLE " + tableName + " CHANGE " + oldFiledName;
                list.addAll(addMySqlFiled(field, ds.getDbType().getCode(), tableName, headSql));
                break;
            case dmsql:
                if (isExistFK(fkName, ds)) {
                    list.add("ALTER TABLE \"" + ds.getLibraryName() + "\".\"" + tableName + "\" drop constraint " +
                            tableName + "_" + field.getFiledName() + "_fk_" + field.getFkTableName() + "_" + field.getFkFieldName() + ";");
                }
                //
                list.add("ALTER TABLE \"" + ds.getLibraryName() + "\".\"" + tableName + "\" drop column \"" + oldFiledName + "\";");
                list.addAll(addFieldSql(field, tableName, ds.getLibraryName(), ds.getDbType()));
                break;
            case sqlserver:
                if (isExistFK(fkName, ds)) {
                    list.add("ALTER TABLE " + tableName + " drop constraint " + tableName + "_" + field.getFiledName() + "_fk_" +
                            field.getFkTableName() + "_" + field.getFkFieldName());
                }
                if (!StringUtils.isNullOrEmpty(oldFiledName)) {
                    list.add("exec sp_rename '" + tableName + "." + oldFiledName + "','" + field.getFiledName() + "'");
                }
                headSql = "ALTER TABLE [dbo].[" + tableName + "] ALTER COLUMN ";
                list.addAll(addSqlServerFiled(field, ds.getDbType().getCode(), tableName, headSql));
                break;
            default:
                return null;
        }
        return list;
    }

    /**
     * 判断外键是否存在
     *
     * @param fkName
     * @param ds
     * @return
     */
    public static boolean isExistFK(String fkName, DataSource ds) {
        //SELECT COUNT(*) FROM `information_schema`.`KEY_COLUMN_USAGE` where constraint_name='name_fk_user_id'
        String str;
        switch (ds.getDbType()) {
            case mysql:
                str = "SELECT COUNT(*) as count FROM `information_schema`.`KEY_COLUMN_USAGE` where constraint_name='" + fkName + "'";
                break;
            case dmsql:
                str = "select count(*) AS COUNT from user_constraints a where a.constraint_type = 'R'  and a.constraint_name = '" + fkName.toUpperCase() + "'";
                break;
            case sqlserver:
                //
                str = "select count(*)  as count from sysobjects where id=object_id('[dbo].[" + fkName + "]') and xtype='F'";
                break;
            default:
                return false;
        }
        return PoolUtils.isExistSql(str, ds);
    }

    /**
     * 查找数据库下所有表名
     *
     * @param baseName
     * @param type
     * @return
     */
    public static String getTableSql(String baseName, int type) {
        String str;
        switch (DbTypeEnum.valueOf(type)) {
            case mysql:
                str = "select table_name from information_schema.tables where table_schema='" + baseName + "';";
                break;
            case dmsql:
                str = "";
                break;
            case sqlserver:
                str = "SELECT Name FROM " + baseName + "..SysObjects Where XType='U' ORDER BY Name";
                break;
            default:
                return null;
        }
        return str;
    }

    /**
     * 查询该表下所有的字段
     *
     * @param baseName
     * @param tableName
     * @param type
     * @return
     */
    public static String getColumnSql(String baseName, String tableName, int type) {
        String str;
        switch (DbTypeEnum.valueOf(type)) {
            case mysql:
                str = "SELECT column_name,column_type,is_nullable,column_key,column_comment FROM information_schema.COLUMNS" +
                        " WHERE table_name = '" + tableName + "' AND table_schema = '" + baseName + "';";
                break;
            case dmsql:
                str = "";
                break;
            case sqlserver:
                //
                str = "SELECT Name FROM " + baseName + "..SysObjects Where XType='U' ORDER BY Name";
                break;
            default:
                return null;
        }
        return str;
    }


    /**
     * 生成多个mysql字段部分语句
     *
     * @param list
     * @return
     */
    public static String baseMySql(List<Field> list, String tableName, int sqlCode) {
        StringBuilder builder = new StringBuilder();
        StringBuilder fk = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            //添加字段名
            builder.append(" `").append(list.get(i).getFiledName()).append("` ");
            String filedType = MySqlDataTypeEnum.getByCode(list.get(i).getFiledType().getCode()).getValue();
            builder.append(filedType);
            //判断是否为主键
            if (list.get(i).isPk()) {
                builder.append("(" + constraintLength + ")");
                builder.append(" NOT NULL");
                builder.append(" PRIMARY KEY");
            } else {
                //判断是否是外键
                if (list.get(i).getFiledType().getCode() == 0) {
                    builder.append("(" + constraintLength + ")");

                    fk.append(",CONSTRAINT " + tableName + "_" + list.get(i).getFiledName() + "_fk_" + list.get(i).getFkTableName() + "_"
                            + list.get(i).getFkFieldName() + " foreign key(" + list.get(i).getFiledName() + ") references `" +
                            list.get(i).getFkTableName() + "`(" + list.get(i).getFkFieldName() + ")\n");
                } else {
                    //判断是否是小数
                    builder.append(sqlIsLength(filedType, list.get(i).getFiledLength(), list.get(i).getFiledAccuracy(), sqlCode));
                }
                //判断是否为空
                if (list.get(i).isNull()) {
                    builder.append(" NULL");
                } else {
                    builder.append(" NOT NULL");
                }
            }
            if (i < (list.size() - 1)) {
                builder.append(",");
            }
            builder.append("\n");
        }
        builder.append(fk);
        return builder.toString();
    }

    /**
     * 生成多个sqlserver字段语句
     *
     * @param list
     * @return
     */
    public static String baseSqlServer(List<Field> list, int sqlCode, String tableName) {
        StringBuilder builder = new StringBuilder();
        StringBuilder fk = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            //添加字段名
            builder.append("   [" + list.get(i).getFiledName() + "] ");
            //获取字段类型字符串
            String filedType = SqlServerDataTypeEnum.getByCode(list.get(i).getFiledType().getCode()).getValue();
            builder.append(filedType);
            //是否是主键
            if (list.get(i).isPk()) {
                builder.append("(" + constraintLength + ")");
                builder.append(" PRIMARY KEY");
                builder.append(" NOT NULL");
            } else {
                //判断是否是外键
                if (list.get(i).getFiledType().getCode() == 0) {
                    builder.append("(" + constraintLength + ")");
                    fk.append(",CONSTRAINT " + tableName + "_" + list.get(i).getFiledName() + "_fk_" + list.get(i).getFkTableName() + "_" +
                            list.get(i).getFkFieldName() + " foreign key(" + list.get(i).getFiledName() + ") references [" +
                            list.get(i).getFkTableName() + "](" + list.get(i).getFkFieldName() + ")\n");
                } else {
                    builder.append(sqlIsLength(filedType, list.get(i).getFiledLength(), list.get(i).getFiledAccuracy(), sqlCode));
                }

                //default '张三'
                if (list.get(i).getFiledType().getCode() == 7) {
                    builder.append(" NOT NULL CONSTRAINT df_" + tableName + "_" + list.get(i).getFiledName() + " default 0");
                } else {
                    if (list.get(i).isNull()) {
                        builder.append(" NULL");
                    } else {
                        builder.append(" NOT NULL");
                    }
                }
            }
            if (i < (list.size() - 1)) {
                builder.append(",");
            }
            builder.append("\n");
        }
        builder.append(fk);
        return builder.toString();
    }

    /**
     * 生成多个达梦表字段部分语句
     *
     * @param list
     * @return
     */
    public static String baseDmServer(List<Field> list, String databaseName, int sqlCode, String tableName) {
        StringBuilder builder = new StringBuilder();
        StringBuilder fk = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            builder.append(" \"").append(list.get(i).getFiledName()).append("\" ");
            //获取字段类型字符串
            String filedType = DmDataTypeEnum.getByCode(list.get(i).getFiledType().getCode()).getValue();
            builder.append(filedType);

            if (list.get(i).isPk()) {
                builder.append("(" + constraintLength + ") PRIMARY KEY UNIQUE NOT NULL");
            } else {
                if (list.get(i).getFiledType().getCode() == 0) {
                    builder.append("(" + constraintLength + ")");
                    //,constraint u_id_fk_user_id foreign key ("u_id") references "test_demo"."tb_user"("id")
                    fk.append(",CONSTRAINT " + tableName + "_" + list.get(i).getFiledName() + "_fk_" +
                            list.get(i).getFkTableName() + "_" + list.get(i).getFkFieldName() +
                            " foreign key(\"" + list.get(i).getFiledName() + "\") references \"" +
                            databaseName + "\".\"" + list.get(i).getFkTableName() + "\"(\"" +
                            list.get(i).getFkFieldName() + "\")\n");
                } else {
                    builder.append(sqlIsLength(filedType, list.get(i).getFiledLength(), list.get(i).getFiledAccuracy(), sqlCode));
                }
                //default '张三'
                if (list.get(i).getFiledType().getCode() == 7) {
                    builder.append(" NOT NULL default 0;");
                } else {
                    if (list.get(i).isNull()) {
                        builder.append(" NULL;");
                    } else {
                        builder.append(" NOT NULL;");
                    }
                }
            }

            if (i < (list.size() - 1)) {
                builder.append(",");
            }
            builder.append("\n");
        }
        builder.append(fk);
        return builder.toString();
    }

    /**
     * mysql 数据库表添加字段
     *
     * @param field
     * @param sqlCode
     * @param tableName
     * @return
     */
    public static List<String> addMySqlFiled(Field field, int sqlCode, String tableName, String sqlHead) {
        List<String> sqlList = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        StringBuilder fk = new StringBuilder();
        builder.append(sqlHead);
        //添加字段名
        builder.append(" `").append(field.getFiledName()).append("` ");
        //获取字段类型字符串
        String filedType = MySqlDataTypeEnum.getByCode(field.getFiledType().getCode()).getValue();
        builder.append(filedType);
        //是否是主键
        if (field.isPk()) {
            builder.append("(" + constraintLength + ")");
            builder.append(" PRIMARY KEY");
            builder.append(" NOT NULL ;");
        } else {
            //判断是否是外键
            if (field.getFiledType().getCode() == 0) {
                builder.append("(" + constraintLength + ")");
                //设置外键语句
                fk.append("ALTER TABLE " + tableName + " add ");
                fk.append("CONSTRAINT " + tableName + "_" + field.getFiledName() + "_fk_" + field.getFkTableName() + "_" + field.getFkFieldName() +
                        " foreign key(" + field.getFiledName() + ") references " +
                        field.getFkTableName() + "(" + field.getFkFieldName() + ") ;");
            } else {
                builder.append(sqlIsLength(filedType, field.getFiledLength(), field.getFiledAccuracy(), sqlCode));
            }

            //default '张三'
            if (field.getFiledType().getCode() == 7) {
                builder.append(" NOT NULL default 0;");
            } else {
                if (field.isNull()) {
                    builder.append(" NULL;");
                } else {
                    builder.append(" NOT NULL;");
                }
            }


        }

        sqlList.add(builder.toString());
        if (fk.length() > 0) {
            sqlList.add(fk.toString());
        }

        return sqlList;
    }

    /**
     * sqlserver数据库表添加字段拼接sql
     *
     * @param field
     * @param sqlCode
     * @param tableName
     * @return
     */
    public static List<String> addSqlServerFiled(Field field, int sqlCode, String tableName, String sqlHead) {
        List<String> sqlList = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        StringBuilder fk = new StringBuilder();

        builder.append(sqlHead);
        //添加字段名
        builder.append("   [" + field.getFiledName() + "] ");
        //获取字段类型字符串
        String filedType = SqlServerDataTypeEnum.getByCode(field.getFiledType().getCode()).getValue();
        builder.append(filedType);
        //是否是主键
        if (field.isPk()) {
            builder.append("(" + constraintLength + ")");
            builder.append(" PRIMARY KEY");
            builder.append(" NOT NULL;");
        } else {
            //判断是否是外键
            if (field.getFiledType().getCode() == 0) {
                fk.append("ALTER TABLE [dbo].[" + tableName + "] add ");
                fk.append("CONSTRAINT " + tableName + "_" + field.getFiledName() + "_fk_" + field.getFkTableName() + "_" + field.getFkFieldName() +
                        " foreign key(" + field.getFiledName() + ") references [" +
                        field.getFkTableName() + "](" + field.getFkFieldName() + ");");
                //设置外键长度
                builder.append("(" + constraintLength + ")");
            } else {
                builder.append(sqlIsLength(filedType, field.getFiledLength(), field.getFiledAccuracy(), sqlCode));
            }
            //default '张三'
            if (field.getFiledType().getCode() == 7) {
                builder.append(" NOT NULL CONSTRAINT df_" + tableName + "_" + field.getFiledName() + " default 0;");
            } else {
                if (field.isNull()) {
                    builder.append(" NULL;");
                } else {
                    builder.append(" NOT NULL;");
                }
            }
        }

        sqlList.add(builder.toString());
        if (fk.length() > 0) {
            sqlList.add(fk.toString());
        }
        return sqlList;
    }


    /**
     * 达梦数据库表添加字段拼接sql
     *
     * @param field
     * @param sqlCode
     * @param databaseName
     * @param tableName
     * @param sqlHead
     * @return
     */
    public static List<String> addDmFiled(Field field, int sqlCode, String databaseName, String tableName, String sqlHead) {
        List<String> sqlList = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        StringBuilder fk = new StringBuilder();

        builder.append(sqlHead);
        //添加字段名
        builder.append(" \"").append(field.getFiledName()).append("\" ");
        //获取字段类型字符串
        String filedType = DmDataTypeEnum.getByCode(field.getFiledType().getCode()).getValue();
        builder.append(filedType);

        if (field.isPk()) {
            builder.append("(" + constraintLength + ") PRIMARY KEY UNIQUE NOT NULL");
        } else {
            //判断是否为外键
            if (field.getFiledType().getCode() == 0) {
                //,constraint u_id_fk_user_id foreign key ("u_id") references "test_demo"."tb_user"("id")
                fk.append("ALTER TABLE \"" + databaseName + "\".\"" + tableName + "\" add ");
                fk.append("CONSTRAINT " + tableName + "_" + field.getFiledName() + "_fk_" +
                        field.getFkTableName() + "_" + field.getFkFieldName() +
                        " foreign key(\"" + field.getFiledName() + "\") references \"" +
                        databaseName + "\".\"" + field.getFkTableName() + "\"(\"" +
                        field.getFkFieldName() + "\");");
                //设置外键长度
                builder.append("(" + constraintLength + ")");
            } else {
                builder.append(sqlIsLength(filedType, field.getFiledLength(), field.getFiledAccuracy(), sqlCode));
            }
            //字段是否为空
            if (field.isNull()) {
                builder.append(" NULL");
            } else {
                builder.append(" NOT NULL");
            }

        }

        sqlList.add(builder.toString());
        if (fk.length() > 0) {
            sqlList.add(fk.toString());
        }
        return sqlList;
    }

    /**
     * 判断sql是否需要列宽
     *
     * @param filedType
     * @param length
     * @return
     */
    public static String sqlIsLength(String filedType, int length, int accuracy, int code) {
        StringBuilder sb = new StringBuilder();
        String str = null;
        //1：mysql  3：sqlserver   4：达梦
        if (code == 3) {
            str = "text,datetime,ntext,tinyint,smallint,int,bigint,bit,image," +
                    "money,smallmoney,smalldatetime,real,date,timestamp,xml";
        } else if (code == 1) {
            str = "date,datetime,tinyint";
        } else if (code == 4) {
            str = "int,tinyint,bigint,date";
        }
        for (String s : str.split(",")) {
            if (filedType.equals(s)) {
                return "";
            }
        }
        sb.append(" (" + length);
        if (filedType.equals("decimal")) {
            //需要小数点精度
            sb.append("," + accuracy);
        }
        sb.append(") ");
        return sb.toString();
    }

    /**
     * 默认逻辑删除，多租户，工作流，父子节点，排序，审核数据拼接
     *
     * @param table
     * @return
     */
    public static List<Field> getListByTable(Table table) {
        List<Field> list = new ArrayList<>();
        List<DefaultFieldEnum> fieldEnums;

        list.addAll(getListByEnum(DefaultFieldEnum.getByParent(0)));

        //是否支持逻辑删除
        if (table.getSupportLogicDelete()) {
            fieldEnums = DefaultFieldEnum.getByParent(1);
            list.addAll(getListByEnum(fieldEnums));
        }
        //是否支持多租户
        if (table.getSupportTenant()) {
            fieldEnums = DefaultFieldEnum.getByParent(2);
            list.addAll(getListByEnum(fieldEnums));
        }
        //是否支持工作流
        if (table.getSupportWorkflow()) {
            fieldEnums = DefaultFieldEnum.getByParent(3);
            list.addAll(getListByEnum(fieldEnums));
        }
        //是否支持排序
        if (table.getSupportSort()) {
            fieldEnums = DefaultFieldEnum.getByParent(4);
            list.addAll(getListByEnum(fieldEnums));
        }
        //是否支持父子结构
        if (table.getSupportParent()) {
            fieldEnums = DefaultFieldEnum.getByParent(5);
            list.addAll(getListByEnum(fieldEnums));
        }
        //是否支持审批
        if (table.getSupportAudit()) {
            fieldEnums = DefaultFieldEnum.getByParent(6);
            list.addAll(getListByEnum(fieldEnums));
        }
        //是否支持操作日志
        if (table.getSupportOperationLog()) {
            fieldEnums = DefaultFieldEnum.getByParent(7);
            list.addAll(getListByEnum(fieldEnums));
        }
        return list;
    }

    //添加字段
    public static List<Field> getListByEnum(List<DefaultFieldEnum> fieldEnums) {
        List<Field> list = new ArrayList<>();
        for (DefaultFieldEnum d : fieldEnums) {
            Field field = new Field();
            field.setFiledName(d.getValue());
            field.setFiledType(d.getDataType());
            field.setFiledLength(d.getLength());
            field.setNull(false);
            field.setSort(d.getIndexNum());
            list.add(field);
        }
        return list;
    }

}
